package com.campus.client.controller;

import com.campus.client.net.ClientConnector;
import com.campus.client.view.PasswordChangeFrame;
import com.campus.client.view.PasswordChangeFrame;
import com.campus.common.message.LoginResponse;
import com.campus.common.message.PasswordChangeResponse;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.util.Objects;

public class PasswordChangeController extends Component {
    private PasswordChangeFrame passwordChangeFrame;
    private ClientConnector connector;

    public PasswordChangeController(PasswordChangeFrame passwordChangeFrame, ClientConnector connector) {
        this.passwordChangeFrame = passwordChangeFrame;
        this.connector = connector;
        setupEventHandlers();
    }

    private void setupEventHandlers() {
        // 设置登录按钮事件
        passwordChangeFrame.setConfirmButtonAction(event -> {
            try {
                passwordChangeControl();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });

        passwordChangeFrame.setCancelButtonAction(event -> {
            try {
                passwordChangeFrame.showMainFrame();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
    }

    private String passwordChangeControl() throws ClassNotFoundException, IOException {

        if(passwordChangeFrame.getOldPassword().isEmpty() || passwordChangeFrame.getNewPassword().isEmpty()) {
            JOptionPane.showMessageDialog(this,
                    "密码不能为空！",
                    "修改失败",
                    JOptionPane.ERROR_MESSAGE);
            return "None";
        }

        if(!Objects.equals(passwordChangeFrame.getOldPassword(), passwordChangeFrame.getPassword())){
            JOptionPane.showMessageDialog(this,
                    "密码错误！",
                    "修改失败",
                    JOptionPane.ERROR_MESSAGE);
            return "None";
        }
        if(Objects.equals(passwordChangeFrame.getOldPassword(), passwordChangeFrame.getNewPassword())){
            JOptionPane.showMessageDialog(this,
                    "新旧密码不能一致！",
                    "修改失败",
                    JOptionPane.ERROR_MESSAGE);
            return "None";
        }


        PasswordChangeResponse resp = connector.changePassword(passwordChangeFrame.getUsername(),passwordChangeFrame.getNewPassword());

        if(resp.isSuccess()){
            passwordChangeFrame.setPassword(passwordChangeFrame.getNewPassword());
            passwordChangeFrame.showMainFrame();

            JOptionPane.showMessageDialog(this,
                    "密码修改成功！",
                    "修改成功",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showMessageDialog(this,
                    "未知的错误！",
                    "修改失败",
                    JOptionPane.ERROR_MESSAGE);
        }

        return resp.getMessage();
    }

}
